var SpringAngularJS = {};

/*module ngRoute using $routeProvider redirect to page
datatables and ngResource : using for datatable table
ui.bootstrap using for tabset => dynamic tab
angularFileUpload using upload file
ui.ace => for editor

*/


var App = angular.module('SpringAngularJS', ['ngRoute', 'ui.bootstrap', 'ngResource', 'datatables' ,'angularFileUpload']);


//Declare app level module which depends on filters, and services
App.config(['$routeProvider', function ($routeProvider){
	
	$routeProvider.when('/profile', {
		templateUrl : 'profile/start',
		controller : ProfileController,
	});
	
	$routeProvider.when('/create', {
		templateUrl : 'create/start',
		controller : CreateController,
	});
	
	$routeProvider.when('/changestatus', {
		templateUrl : 'changestatus/start',
		controller : ChangeStatusController,
	});
	
	$routeProvider.when('/history', {
		templateUrl : 'history/start',
		controller : HistoryController,
	});
	
	$routeProvider.when('/updateinformation', {
		templateUrl : 'updateinfo/start',
		controller : UpdateController,
	});
	
}]);

/*JQuery plugin validate dynamic form*/
App.directive('jValidateUpdate', function() {

	  return {
	    link: function(scope, element, attr) {

	      element.validate({

	    	  rules: {
					/* Get follow name */
					password: {
						required : false,
						minlength : 8
					},
					password_again: {
						required : false,
						minlength : 8,
						equalTo: "#password"
					},
					cardnumberid: {
						required : true,
						remote : {
							url: "http://localhost:8080/finalproject/create/checkcardnumber",
							type: 'post',
							data: {
								cardnumberid: function(){
									return $("#cardnumberid").val();
								}
							}
						}
					},
					firstname: {
						required : true,
						remote : {
							url: "http://localhost:8080/finalproject/create/checkfirstname",
							type: 'post',
							data: {
								firstname: function(){
									return $("#firstname").val();
								}
							}
						}
					},
					lastname: {
						required : true,
						remote : {
							url: "http://localhost:8080/finalproject/create/checklastname",
							type: 'post',
							data: {
								lastname: function(){
									return $("#lastname").val();
								}
							}
						}
					},
					midname: {
						required : false,
						remote : {
							url: "http://localhost:8080/finalproject/create/checkmidname",
							type: 'post',
							data: {
								midname: function(){
									return $("#midname").val();
								}
							}
						}
					},
					phone1:{
						required: true,
						remote: {
							url: "http://localhost:8080/finalproject/create/checkphonefirst",
							type: 'post',
							data: {
								phone1: function(){
									return $("#phone1").val();
								}
							}
						}
					},
					phone2:{
						required: false,
						remote: {
							url: "http://localhost:8080/finalproject/create/checkphonesecond",
							type: 'post',
							data: {
								phone2: function(){
									return $("#phone2").val();
								}
							}
						}
					},
					address1: {
						required: true
					},
					email1 : {
						required : true,
						email : true
					},
					email2 : {
						required : false,
						email : true
					}
				},
				messages : {
					loginId : {
						remote : "User with login Id already exist !"
					},
					cardnumberid: {
						remote : "Card number Id must 9 numberic. Example: 135548878"
					},
					firstname: {
						remote : "Name only contains words"
					},
					lastname: {
						remote : "Name only contains words"
					},
					midname: {
						remote : "Name only contains words"
					},
					phone1 : {
						remote : "Phone only digit 10 or 11 number"
					},
					phone2 : {
						remote : "Phone only digit 10 or 11 number"
					}
				},
	        submitHandler: function(form) {
	          console.log('Valid');
	        },
	        invalidHandler: function(event, validator) {
	          console.log('InValid');
	        }
	      });
	      
	      scope.$on('$destroy', function () {
	        // Perform cleanup.
	        // (Not familiar with the plugin so don't know what should to be done)
	      });
	    }
	  }
});

/*JQuery plugin validate dynamic form*/
App.directive('jValidateCreate', function() {

	  return {
	    link: function(scope, element, attr) {

	      element.validate({

	    	  rules: {
					/* Get follow name */
					loginId: {
						required : true,
						minlength : 5,
						remote : {
							url: "http://localhost:8080/finalproject/create/checkLoginId",
							type: 'post',
							data: {
								loginId: function(){
									return $("#loginId").val();
								}
							}
						}
					},
					password: {
						required : true,
						minlength : 8
					},
					password_again: {
						required : true,
						minlength : 8,
						equalTo: "#password"
					},
					cardnumberid: {
						required : true,
						remote : {
							url: "http://localhost:8080/finalproject/create/checkcardnumber",
							type: 'post',
							data: {
								cardnumberid: function(){
									return $("#cardnumberid").val();
								}
							}
						}
					},
					firstname: {
						required : true,
						remote : {
							url: "http://localhost:8080/finalproject/create/checkfirstname",
							type: 'post',
							data: {
								firstname: function(){
									return $("#firstname").val();
								}
							}
						}
					},
					lastname: {
						required : true,
						remote : {
							url: "http://localhost:8080/finalproject/create/checklastname",
							type: 'post',
							data: {
								lastname: function(){
									return $("#lastname").val();
								}
							}
						}
					},
					midname: {
						required : false,
						remote : {
							url: "http://localhost:8080/finalproject/create/checkmidname",
							type: 'post',
							data: {
								midname: function(){
									return $("#midname").val();
								}
							}
						}
					},
					phone1:{
						required: true,
						remote: {
							url: "http://localhost:8080/finalproject/create/checkphonefirst",
							type: 'post',
							data: {
								phone1: function(){
									return $("#phone1").val();
								}
							}
						}
					},
					phone2:{
						required: false,
						remote: {
							url: "http://localhost:8080/finalproject/create/checkphonesecond",
							type: 'post',
							data: {
								phone2: function(){
									return $("#phone2").val();
								}
							}
						}
					},
					address1: {
						required: true
					},
					email1 : {
						required : true,
						email : true
					},
					email2 : {
						required : false,
						email : true
					}
				},
				messages : {
					loginId : {
						remote : "User with login Id already exist !"
					},
					cardnumberid: {
						remote : "Card number Id must 9 numberic. Example: 135548878"
					},
					firstname: {
						remote : "Name only contains words"
					},
					lastname: {
						remote : "Name only contains words"
					},
					midname: {
						remote : "Name only contains words"
					},
					phone1 : {
						remote : "Phone only digit 10 or 11 number"
					},
					phone2 : {
						remote : "Phone only digit 10 or 11 number"
					}
				},
	        submitHandler: function(form) {
	          console.log('Valid');
	        },
	        invalidHandler: function(event, validator) {
	          console.log('InValid');
	        }
	      });
	      
	      scope.$on('$destroy', function () {
	        // Perform cleanup.
	        // (Not familiar with the plugin so don't know what should to be done)
	      });
	    }
	  }
});